//
//  LeftSortsViewController.m
//  LGDeckViewController
//
//  Created by jamie on 15/3/31.
//  Copyright (c) 2015年 Jamie-Ling. All rights reserved.
//

#import "LeftSortsViewController.h"
#import "AppDelegate.h"
#import "otherViewController.h"
#import "RestoreViewController.h"
#import <SDImageCache.h>
#import <DKNightVersion/DKImage.h>
@interface LeftSortsViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, strong)UITableView *tableView;

@end

@implementation LeftSortsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.array = [NSMutableArray arrayWithObjects:@"我的收藏", @"我的MV", @"我的Music", @"夜间模式",@"清理缓存",nil];
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.dk_backgroundColorPicker = DKColorPickerWithRGB(0xAEEEEE, 0x00868B);
//    imageview.image = [UIImage imageNamed:@"84D59CDFEB23801486C91D2E5EF016C5"];
    UIImage *image1 = [UIImage imageNamed:@"84D59CDFEB23801486C91D2E5EF016C5"];
    UIImage *image2 = [UIImage imageNamed:@"EB1E730D0876521EDB64357D161CBBFC"];
    NSLog(@"===%@ %@",image1,image2);
    imageview.dk_imagePicker = [DKImage pickerWithNormalImage:image1 nightImage:image2];
//    imageview.image = [UIImage imageNamed:@"84D59CDFEB23801486C91D2E5EF016C5"];
    [self.view addSubview:imageview];

    UITableView *tableview = [[UITableView alloc] init];
    self.tableview = tableview;
    tableview.frame = self.view.bounds;
    tableview.dataSource = self;
    tableview.delegate  = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = tableview;
    [self.view addSubview:tableview];
    self.tableView.backgroundColor = [UIColor clearColor];
//     DKColorPickerWithRGB(0x7FFF00, 0x8B4513);
//    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    cell.textLabel.text = self.array[indexPath.row];
    if (indexPath.row == 4) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"共缓存%.2fM",[self totalSize]];
    }
//    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0x7FFF00, 0x8B4513);
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = 0;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        RestoreViewController *restoreVC = [RestoreViewController new];
        [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNAVC pushViewController:restoreVC animated:NO];
    } else if (indexPath.row == 3) {
        if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
            NSLog(@"白天");
            [self.dk_manager dawnComing];
        } else {
            NSLog(@"晚上");
            
            [self.dk_manager nightFalling];
        }
    } else if (indexPath.row == 4) {
        [[SDImageCache sharedImageCache] clearDisk];  //清楚磁盘缓存
        [self.tableView reloadData];
    }
    
//    otherViewController *vc = [[otherViewController alloc] init];
//    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
//    UIViewController *mianVC = tempAppDelegate.LeftSlideVC.mainVC;
//    [mianVC.view addSubview:vc.view];
//    vc.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
//    vc.view.backgroundColor = [UIColor redColor];
    

    
//    [tempAppDelegate.mainNavigationController pushViewController:vc animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 180)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}



// 清理缓存
//1.计算文件大小：

- (long long) fileSizeAtPath:(NSString*) filePath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath]){
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
        
    }
    
    return 0;
    
}

//2.计算文件夹大小:

//遍历文件夹获得文件夹大小，返回多少M

- (float ) folderSizeAtPath:(NSString*) folderPath{
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:folderPath]) return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString* fileName;
    
    long long folderSize = 0;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
        
    }
    
    return folderSize/(1024.0*1024.0);
    
}

// 沙盒内的所有缓存大小:
- (float)totalSize {
   
    float totalSize = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()] + [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject];
    return totalSize;
}



@end
